/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include <MkGb28181/MkGbEndUser.h>
#include <MkUtil/MkLog.h>
#include <MkEvent/MkEventPool.h>
#include <MkEvent/MkTimer.h>
#include <MkEvent/MkTimerPool.h>
#include <MkGbMedia/MkGbMediaEndUser.h>
#include <MkMedia/MkFileSource.h>

int main(int argc, char** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
    MkString FileName = "C:/Workspace/media/aa.264";
#else
    MkString FileName = "/root/wzg/mksource/bin/media/aa.264";
#endif

    MkString Ip = "192.168.100.120";
    Ip = "192.168.0.24";
    Uint16 Port = 5080;
    int nCount = 1;
    MkLogChangeLevel(LOG_DEBUG);
    MkLogChangeWriteDst(TO_SCREEN);
    CMkEventPool* pEventPool = new CMkEventPool;
    pEventPool->Start();
    CMkTimer *pTimer = new CMkTimer;
    pTimer->StartTimerThread();

    CMkMediaSource::SetCachePacket(FALSE);

    CMkGbMediaEndUser *pGbEndUser = new CMkGbMediaEndUser(pEventPool, pTimer);
    MkGbStartParam Param;
    Param.LocalHost = "192.168.0.24";
    Param.LocalPort = 55560;
    Param.SipId = "34020000004000000001";
    Param.SipRealm = "3402000000";
    Param.MappingHost = "";
    Param.MappingPort = 0;
    Param.RegisterExpires = 3600;
    Param.SubscribeExpires = 3600;
    pGbEndUser->Start(Param, 34000);

    MkString SipUri;
    char GetC;
    while (GetC = getchar()) {
        BOOL bQuit = FALSE;
        SipUri = "sip:3402000000132000000" + MkString(&GetC) + "@" + Ip + ":" + std::to_string(Port);
        FILE *pTmpFp = fopen(FileName.c_str(), "rb");
        CMkH264FileSource *pFileSource = new CMkH264FileSource(pTmpFp, pTimer);
        switch (GetC) {
        case 'q':
            bQuit = TRUE;
            break;
        case '1':
            pGbEndUser->Publish(SipUri, FALSE, FALSE, pFileSource, [pFileSource](Uint32 ErrorCode, const MkString& SessionId) {
                MkDebugLog("start publish error code:%d session:%s\n", ErrorCode, SessionId.c_str());
                pFileSource->StartMediaSource(nullptr);
            }, [pFileSource](const MkString& SessionId) {
                MkDebugLog("Session:%s has stoped\n", SessionId.c_str());
                pFileSource->StopMediaSource();
            });
            break;
        case '2':
            pGbEndUser->Publish2(SipUri, FALSE, FALSE, pFileSource, [pFileSource](Uint32 ErrorCode, const MkString& SessionId) {
                MkDebugLog("start publish error code:%d session:%s\n", ErrorCode, SessionId.c_str());
                pFileSource->StartMediaSource(nullptr);
            }, [pFileSource](const MkString& SessionId) {
                MkDebugLog("Session:%s has stoped\n", SessionId.c_str());
                pFileSource->StopMediaSource();
            });
        default:
            break;
        }
        if (bQuit) {
            break;
        }
    }
    pTimer->StopTimerThread();
    pEventPool->Stop();
    MkDelete(pEventPool);
    MkDelete(pTimer);
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}